import json
from pymongo import Connection
from datetime import datetime

class Order:

	def __init__(self, host, port, db, table):
		self.connection = Connection(host, port)
		self.db = db
		self.table = table
		
	def postOrder(self, data):
		entity = json.loads(data)
		entity['timestamp'] = datetime.now()
		try:
			self.db[self.table].save(entity)
			return 200
		except:
			return 400

	def getOrderByOrderID(self, orderID):
		entity = self.db[self.table].find_one({'orderID':int(orderID)})
		if not entity:
			return 404
		del entity['_id']
		del entity['timestamp']
		return entity